import time

import requests
from lxml import etree


headers = {
    "Accept": "*/*",
    "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
    "Connection": "keep-alive",
    "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
    "Origin": "https://kns.cnki.net",
    "Referer": "https://kns.cnki.net/kns8s/defaultresult/index?korder=&kw=",
    "Sec-Fetch-Dest": "empty",
    "Sec-Fetch-Mode": "cors",
    "Sec-Fetch-Site": "same-origin",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0",
    "X-Requested-With": "XMLHttpRequest",
    "sec-ch-ua": "\"Not)A;Brand\";v=\"8\", \"Chromium\";v=\"138\", \"Microsoft Edge\";v=\"138\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\""
}
url = "https://kns.cnki.net/kns8s/brief/grid"
total = 0
for page in range(1, 121):
    data = {
        "boolSearch": "true",
        "QueryJson": "{\"Platform\":\"\",\"Resource\":\"CROSSDB\",\"Classid\":\"WD0FTY92\",\"Products\":\"CJFQ,CAPJ,CJTL,CDFD,CMFD,CPFD,IPFD,CPVD,CCND,WBFD,SCSF,SCHF,SCSD,SNAD,CCJD,CJFN,CCVD\",\"QNode\":{\"QGroup\":[{\"Key\":\"Subject\",\"Title\":\"\",\"Logic\":0,\"Items\":[{\"Field\":\"SU\",\"Value\":\"科技\",\"Operator\":\"TOPRANK\",\"Logic\":0,\"Title\":\"主题\"}],\"ChildItems\":[]},{\"Key\":\"SCDBGroup\",\"Title\":\"\",\"Logic\":0,\"Items\":[],\"ChildItems\":[{\"Key\":\"ZYZT|||CYZT\",\"Title\":\"\",\"Logic\":0,\"Items\":[{\"Key\":\"科技创新\",\"Title\":\"科技创新\",\"Logic\":1,\"Field\":\"ZYZT\",\"Operator\":\"DEFAULT\",\"Value\":\"科技创新\",\"Value2\":\"\",\"Name\":\"ZYZT\"}],\"ChildItems\":[]},{\"Key\":\"CCL\",\"Title\":\"\",\"Logic\":0,\"Items\":[{\"Key\":\"J147?\",\"Title\":\"经济体制改革\",\"Logic\":1,\"Field\":\"CCL\",\"Operator\":\"DEFAULT\",\"Value\":\"J147?\",\"Value2\":\"\",\"Name\":\"CCL\",\"ExtendType\":0}],\"ChildItems\":[]},{\"Key\":\"YE\",\"Title\":\"\",\"Logic\":0,\"Items\":[{\"Key\":\"2025\",\"Title\":\"2025年\",\"Logic\":1,\"Field\":\"YE\",\"Operator\":\"DEFAULT\",\"Value\":\"2025\",\"Value2\":\"\",\"Name\":\"YE\",\"ExtendType\":0}],\"ChildItems\":[]}]}]},\"ExScope\":1,\"SearchType\":2,\"Rlang\":\"CHINESE\",\"KuaKuCode\":\"YSTT4HG0,LSTPFY1C,JUP3MUPD,MPMFIG1A,EMRPGLPA,WQ0UVIAA,BLZOG7CK,PWFIRAGL,NN3FJMUV,NLBO1Z6R\",\"Expands\":{},\"SearchFrom\":2}",
        "pageNum": "1",
        "pageSize": "20",
        "dstyle": "listmode",
        "boolSortSearch": "false",
        "productStr": "YSTT4HG0,LSTPFY1C,RMJLXHZ3,JQIRZIYA,JUP3MUPD,1UR4K4HZ,BPBAFJ5S,R79MZMCB,MPMFIG1A,EMRPGLPA,J708GVCE,ML4DRIDX,WQ0UVIAA,NB3BWEHK,XVLO76FD,HR1YT1Z9,BLZOG7CK,PWFIRAGL,NN3FJMUV,NLBO1Z6R,",
        "aside": "主题：科技",
        "searchFrom": "资源范围：总库",
        "subject": "",
        "language": "",
        "uniplatform": "",
        "CurPage": "1"
    }
    response = requests.post(url, headers=headers, data=data)
    html_str = response.content.decode()
    root = etree.HTML(html_str)

    essay_list = root.xpath("//table[@class='result-table-list']/tbody/tr")
    for essay in essay_list:
        total += 1
        title = "".join(essay.xpath("./td[@class='name']/a//text()"))
        author = ",".join(essay.xpath("./td[@class='author']//a/text()"))
        score = "".join(essay.xpath("./td[@class='score']//a/text()"))
        date = "".join(essay.xpath("./td[@class='date']/text()"))
        data = "".join(essay.xpath("./td[@class='data']/span/text()"))
        quote = "".join(essay.xpath("./td[@class='quote']/span/span/text()")) if essay.xpath("./td[@class='quote']/span/span/text()") else "暂无"
        download = "".join(essay.xpath("./td[@class='download']//a/text()")) if essay.xpath("./td[@class='download']//a/text()") else "暂无"
        print(page, total, title, author, score, date, data, quote, download)



